<?php

namespace app\common\model;

use think\Model;


class Lesson extends Model
{

    

    //数据库
    protected $connection = 'database';
    // 表名
    protected $name = 'lesson';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'lesson_start_time_text',
        'lesson_end_time_text',
		'start_time_text',
		'end_time_text',
        'indexswitch_text',
        'category_list_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getIndexswitchList()
    {
        return ['0' => __('Indexswitch 0'), '1' => __('Indexswitch 1')];
    }

    public function getCategoryListList()
    {
        return ['1' => __('Category_list 1'), '2' => __('Category_list 2'), '3' => __('Category_list 3')];
    }


    public function getLessonStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['lesson_start_time']) ? $data['lesson_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getLessonEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['lesson_end_time']) ? $data['lesson_end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }




    public function getIndexswitchTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['indexswitch']) ? $data['indexswitch'] : '');
        $list = $this->getIndexswitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCategoryListTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['category_list']) ? $data['category_list'] : '');
        $list = $this->getCategoryListList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setLessonStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setLessonEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function lessongroup()
    {
        return $this->belongsTo('LessonGroup', 'group_list', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
